﻿using CPPEI.ETL.DataServiceInterface;
using CPPEI.WebApi;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using ENT = CPPEI.ETL.Domain.Entities;
using MOD = CPPEI.ETL.Domain.Models;
using SER = CPPEI.ETL.Domain.Services;

namespace CPPEI.ETL.WebApi
{
    /// <summary>
    /// 枚举可用的数据服务
    /// </summary>
    [CPPEI.WebApi.AuthGetAction("DS", "EnumeratePubDataSource", "1.0")]
    public class EnumeratePubDataSourceAction : BaseAction<EnumeratePubDataSourceParams, EnumeratePubDataSourceResult>
    {
        protected override ActionResult<EnumeratePubDataSourceResult> OnExecute(ActionContext context,
            SysParameters sysParameters,
            EnumeratePubDataSourceParams opParams)
        {
            SER.DtSerService service = new SER.DtSerService();
            ListResult<TDataSource> serResult = service.EnumerateDataSources(opParams.DataServiceId);
            if (serResult.IsSuccess)
            {
                EnumeratePubDataSourceResult result = new EnumeratePubDataSourceResult();

                for (int index = 0; index < serResult.Data.Count; index++)
                {
                    EnumeratePubDataSourceResult.Item item = new EnumeratePubDataSourceResult.Item();
                    item.Name = serResult.Data[index].Name;
                    item.Description = DefaultValue.GetValue(serResult.Data[index].Description, string.Empty);
                    item.Value = DefaultValue.GetValue(serResult.Data[index].Value, string.Empty);

                    result.Items.Add(item);
                }
                return new ActionResult<EnumeratePubDataSourceResult>(result);
            }
            else
            {
                return new ActionResult<EnumeratePubDataSourceResult>(serResult.IsSuccess, serResult.ErrorCode, serResult.Message, null);
            }
        }
    }

    public class EnumeratePubDataSourceParams
    {
        public Guid DataServiceId { get; set; }
    }

    public class EnumeratePubDataSourceResult
    {
        public List<Item> Items = new List<Item>();

        public class Item
        {
            public string Value { get; set; }

            public string Name { get; set; }

            public string Description { get; set; }
        }
    }
}
